﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utils.Common;
using ZK.BLL;
using ZK.Entity;

namespace ZK.Client.WinForm
{
    public partial class FrmCIJTemplateEdit : Form
    {
        public FrmCIJTemplateEdit()
        {
            InitializeComponent();
        }

        public CIJTemplate CIJTemplate { get; set; }

        private void FrmCIJTemplateEdit_Load(object sender, EventArgs e)
        {
         
            if (CIJTemplate != null)
            {
                txtTemplateName.Text = CIJTemplate.TemplateName;

                txtP1Font.Text = CIJTemplate.P1Font;
                ucP1OffSetX.Value = CIJTemplate.P1OffSetX ;
                ucP1OffSetY.Value = CIJTemplate.P1OffSetY;
                ucP1Unit.Value = CIJTemplate.P1Unit;
             

                txtP2Font.Text = CIJTemplate.P2Font;
                ucP2OffSetX.Value = CIJTemplate.P2OffSetX;
                ucP2OffSetY.Value = CIJTemplate.P2OffSetY;
                ucP2Unit.Value = CIJTemplate.P2Unit;
             

                txtP3Font.Text = CIJTemplate.P3Font;
                ucP3OffSetX.Value = CIJTemplate.P3OffSetX;
                ucP3OffSetY.Value = CIJTemplate.P3OffSetY;
                ucP3Unit.Value = CIJTemplate.P3Unit;
          

            }
            else {
                CIJTemplate = new CIJTemplate();
            }

          
        }

        private void BtnFont_Click(object sender, EventArgs e)
        {
            var btn = (Button)sender;
            FontDialog fontDialog = new FontDialog();
            if (!string.IsNullOrEmpty(btn.Text))
            {
             
                fontDialog.Font = CIJTemplateBLL.Instance.GetFont(btn.Text);
            }
            if (fontDialog.ShowDialog() == DialogResult.OK)
            {
                btn.Text = CIJTemplateBLL.Instance.Font2String(fontDialog.Font);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSure_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtTemplateName.Text.Trim()))
            {
                MessageBox.Show("请输入模板名称");
                return;
            }


            if (string.IsNullOrEmpty(txtP1Font.Text))
            {
                MessageBox.Show("请设置型号规格字体");
                return;
            }

            if (string.IsNullOrEmpty(txtP2Font.Text))
            {
                MessageBox.Show("请设置批次号字体");
                return;
            }
            if (string.IsNullOrEmpty(txtP3Font.Text))
            {
                MessageBox.Show("请设置附加信息字体");
                return;
            }

            if (ucP1Unit.Value <= 0|| ucP2Unit.Value <= 0|| ucP3Unit.Value <= 0)
            {
                MessageBox.Show("单位像素必须为正整数！");
                return;
            }

            CIJTemplate.TemplateName = txtTemplateName.Text.Trim();
            CIJTemplate.P1Font = txtP1Font.Text.Trim();
            CIJTemplate.P1Unit = ucP1Unit.Value;
         
            CIJTemplate.P1OffSetX = ucP1OffSetX.Value;
            CIJTemplate.P1OffSetY = ucP1OffSetY.Value;

            CIJTemplate.P2Font = txtP2Font.Text.Trim();
            CIJTemplate.P2Unit = ucP2Unit.Value;

            CIJTemplate.P2OffSetX = ucP2OffSetX.Value;
            CIJTemplate.P2OffSetY = ucP2OffSetY.Value;

            CIJTemplate.P3Font = txtP3Font.Text.Trim();
            CIJTemplate.P3Unit = ucP3Unit.Value;
     
            CIJTemplate.P3OffSetX = ucP3OffSetX.Value;
            CIJTemplate.P3OffSetY = ucP3OffSetY.Value;


            if (string.IsNullOrEmpty(CIJTemplate.Id))
            {

                CIJTemplate.Id = CIJTemplateBLL.Instance.GetNewID();
                if (CIJTemplateBLL.Instance.Insert(CIJTemplate))
                {
                    this.DialogResult = DialogResult.OK;

                    this.Close();
                }
                else
                {
                    MessageBox.Show("保存失败");
                }
            }
            else {

                CIJTemplateBLL.Instance.DeleteById(CIJTemplate.Id);
                if (CIJTemplateBLL.Instance.Insert(CIJTemplate))
                {
                    this.DialogResult = DialogResult.OK;

                    this.Close();
                }
                else
                {
                    MessageBox.Show("保存失败");
                }
            }
         
        }

        



    }
}
